package com.yunmang.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yunmang.R;
import com.yunmang.view.CustomDialog;

/**
 * 作者 : bean on 2017/4/5/0005.
 * 注释 :
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener{
    private CustomDialog mDialogWaiting;
    public Context context;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.context = getActivity();
        View view = View.inflate(context, getLayoutId(), null);
        initView(view);
        return view;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        dealCommon();
    }

    public abstract int getLayoutId() ;

    public abstract void initView(View view);

    public abstract void initData();

    public  void dealCommon(){}

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            default:
                onInnerClick(v);
                break;
        }
    }
    /**
     * 显示等待提示框
     */
    public Dialog showWaitingDialog(String tip) {
        hideWaitingDialog();
        View view = View.inflate(getActivity(), R.layout.dialog_waiting, null);
        if (!TextUtils.isEmpty(tip))
            ((TextView) view.findViewById(R.id.tvTip)).setText(tip);
        mDialogWaiting = new CustomDialog(getActivity(), view, R.style.dialog);
        mDialogWaiting.show();
        return mDialogWaiting;
    }

    /**
     * 隐藏等待提示框
     */
    public void hideWaitingDialog() {
        if (mDialogWaiting != null) {
            mDialogWaiting.dismiss();
            mDialogWaiting = null;
        }
    }
    public void onInnerClick(View v){}
    public void showToast(String title) {
        Toast.makeText(context,title,Toast.LENGTH_SHORT).show();
    }
}
